package com.aaa.system.service.impl;

import com.aaa.entity.LoginInfo;
import com.aaa.system.dao.LoginInfoDao;
import com.aaa.system.service.ILoginInfoService;
import com.aaa.system.vo.LoginInfoVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * <p>
 * 系统访问记录 服务实现类
 * </p>
 *
 * @author L
 * @since 2023-03-24
 */
@Service
public class LoginInfoServiceImpl extends ServiceImpl<LoginInfoDao, LoginInfo> implements ILoginInfoService {

    @Autowired
    private LoginInfoDao loginInfoDao;

    @Override
    public Result<IPage<LoginInfo>> selectAll(Integer current, Integer size, LoginInfoVo loginInfoVo) {
        IPage<LoginInfo> page = new Page<>(current,size);
        QueryWrapper<LoginInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(loginInfoVo.getUserName())){
            queryWrapper.like("user_name",loginInfoVo.getUserName());
        }
        if (StringUtils.hasText(loginInfoVo.getLoginAccount())){
            queryWrapper.like("login_account",loginInfoVo.getLoginAccount());
        }
        if (StringUtils.hasText(loginInfoVo.getIpAddr())){
            queryWrapper.eq("ip_addr",loginInfoVo.getIpAddr());
        }
        if (Objects.nonNull(loginInfoVo.getLoginStatus())){
            queryWrapper.eq("login_status",loginInfoVo.getLoginStatus());
        }
        if (Objects.nonNull(loginInfoVo.getLoginType())){
            queryWrapper.eq("login_type",loginInfoVo.getLoginType());
        }
        if (Objects.nonNull(loginInfoVo.getDateRange()) && loginInfoVo.getDateRange().length==2){
            queryWrapper.between("login_time",loginInfoVo.getDateRange()[0],loginInfoVo.getDateRange()[1]);
        }
        IPage<LoginInfo> loginInfoIPage = loginInfoDao.selectPage(page,queryWrapper);
        return new Result<>(200,"成功查询所有登录日志",loginInfoIPage);
    }

    @Override
    public Result<Boolean> deleteLoginInfoByIds(Long[] infoIds) {
        Boolean b = null;
        if (infoIds.length>0){
            b = loginInfoDao.batchInfoById(infoIds);
        }
        return new Result<>(200,"删除成功",b);
    }

    @Override
    public Result<Boolean> truncateTable() {
        Boolean b = loginInfoDao.truncateTable();
        return new Result<>(200,"清空所有日志成功",b);
    }

    @Override
    public void addLoginInfo(LoginInfo loginInfo) {
        loginInfoDao.insert(loginInfo);
    }
}
